package com.ibiaoke.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.ibiaoke.common.configs.AliPayConfigs;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import static com.ibiaoke.common.configs.AliPayConfigs.HTTP_METHOD;

@Component
public class BkAliPayClient {

    private Logger logger = Logger.getLogger(this.getClass().getName());

    public String aliPay(String out_trade_no, String subject, String total_amount, String notify_url, String return_url) {
        String product_code = "QUICK_WAP_WAY";
        AlipayClient alipayClient = new DefaultAlipayClient(AliPayConfigs.getGateWay(), AliPayConfigs.getAppID(),
                AliPayConfigs.getPrivateKey(),AliPayConfigs.FORMAT ,AliPayConfigs.CHARSET, AliPayConfigs.getAliPublicKey(),AliPayConfigs.SIGNTYPE);
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setReturnUrl(return_url);
        request.setNotifyUrl(notify_url);//在公共参数中设置回跳和通知地址
        // 封装请求支付信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        model.setOutTradeNo(out_trade_no);
        model.setSubject(subject);
        model.setTotalAmount(total_amount);
        model.setBody("");
        model.setTimeoutExpress("2m");
        model.setProductCode(product_code);
        request.setBizModel(model);
        String form = null; //调用SDK生成表单
        try {
            form = alipayClient.pageExecute(request,HTTP_METHOD).getBody();
        } catch (AlipayApiException e) {
            logger.error(e);
        }
        return form;
    }
}
